<#import "commons.ftl" as m/>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="<@m.getBasePackage param=context flag=true/><@m.getDalPackage param=context flag=true/>${table.upperName}<@m.getDaoSuffix param=context/>">

    <insert id="insert${table.upperName}" parameterType="<@m.getBasePackage param=context flag=true/><@m.getModelPackage param=context flag=true/>${table.upperName}<@m.getModelSuffix param=context/>" useGeneratedKeys="true" keyProperty="id">
     INSERT INTO ${table.origName}
     (<#if table.dataList??>
    	<#list (table.dataList) as column>
    	<#if column.name!='id'>
    	${column.name}<#if (column_index+1)!=(table.dataList)?size >,</#if>
    	</#if>
		</#list>
     </#if>) 
      VALUES 
     (
    <#if table.dataList??>
    <#list (table.dataList) as column>
    	<#if column.name!='id'>
    	<#if (column.lowerName=='createTime')||(column.lowerName=='updateTime')>
    	SYSDATE()<#if (column_index+1)!=(table.dataList)?size >,</#if>
    	<#else>
    	${"#{"+column.lowerName+"}"}<#if (column_index+1)!=(table.dataList)?size >,</#if>
    	</#if>
    	</#if>
     </#list>
     </#if>
     )
    </insert>
  
    <delete id="deleteById" parameterType="${table.pkType!'Long'}">
      DELETE FROM ${table.origName} WHERE id = ${"#{"+_abcde!'value'+"}"}
    </delete>
  	
  	<sql id="pageWhere">
		<where>
	    </where>
  	</sql>
  	  
    <select id="countPage" resultType="int">
     SELECT count(*) FROM ${table.origName}
 	  <include refid="pageWhere"/>
    </select>
  
    <select id="queryPage" resultType="<@m.getBasePackage param=context flag=true/><@m.getModelPackage param=context flag=true/>${table.upperName}<@m.getModelSuffix param=context/>">
      SELECT 
    <#if table.dataList??>
    	<#list (table.dataList) as column>
    	${column.name}<#if column.name!=column.lowerName> ${column.lowerName}</#if><#if (column_index+1)!=(table.dataList)?size >,</#if>
		</#list>
     </#if>
      FROM ${table.origName}
      <include refid="pageWhere"/>
      ORDER BY id DESC
      LIMIT ${"#{"+_abcde!'startRow'+"}"},${"#{"+_abcde!'pageSize'+"}"}
    </select>
  
    <select id="queryById" parameterType="${table.pkType!'Long'}" resultType="<@m.getBasePackage param=context flag=true/><@m.getModelPackage param=context flag=true/>${table.upperName}<@m.getModelSuffix param=context/>">
      SELECT
    <#if table.dataList??>
    	<#list (table.dataList) as column>
    	${column.name}<#if column.name!=column.lowerName> ${column.lowerName}</#if><#if (column_index+1)!=(table.dataList)?size >,</#if>
		</#list>
     </#if>
      FROM ${table.origName}
      WHERE id = ${"#{"+_abcde!'value'+"}"}
    </select>
  
    <update id="update${table.upperName}" parameterType="<@m.getBasePackage param=context flag=true/><@m.getModelPackage param=context flag=true/>${table.upperName}<@m.getModelSuffix param=context/>">
      UPDATE ${table.origName}
      SET
    <#if table.dataList??>
    	<#list (table.dataList) as column>
    		<#if (column.lowerName!='createTime')&&(column.lowerName!='id')&&(column.lowerName!='createdBy')>
    			<#if (column.lowerName=='updateTime')>
    	${column.name} = SYSDATE()<#if (column_index+1)!=(table.dataList)?size >,</#if>
    			<#else>
    	${column.name} = ${"#{"+column.lowerName+"}"}<#if (column_index+1)!=(table.dataList)?size >,</#if>
    			</#if>
    		</#if>
		</#list>
     </#if>
      WHERE id = ${"#{"+_abcde!'id'+"}"}
    </update>
  
</mapper>
